<!DOCTYPE html>
<html>

<head>
    <title>Preview Display</title>
    <meta charset="UTF-8">
    <!--    <link rel="shortcut icon" href="custom/images/favicon.ico?v=2"/>-->
    <style>
        html,
        body {
            padding: 0px;
            margin: 0px;
        }

        body {
            background: #000 url(../storage/assets/兴达八厂/车间背景.jpg) no-repeat left top;
            background-size: 100%;
        }
    </style>
    <script src="../libs/jquery-3.4.1.min.js"></script>
    <script src='../libs/message.js'></script>
    <!--    <script src='../custom/configs/htconfig.js'></script>-->
    <script src='../custom/libs/echarts.js'></script>
    <script src='../libs/core/ht.js'></script>
    <script src='../libs/core/ht-ui.js'></script>
    <script src='../libs/plugin/ht-edgetype.js'></script>
    <script src='../libs/plugin/ht-obj.js'></script>
    <script src='../libs/plugin/ht-form.js'></script>
    <script src='../libs/plugin/ht-vector.js'></script>
    <script src='../conf/confURL.js'></script>
    <script src='../conf/dateTime.js'></script>
    <script src='../conf/workshopAjax.js'></script>
     <script>
         //color.js报警配置文件
         document.write('<scr' + 'ipt src="' + layout_editorURL + '/config/color.js"></scr' + 'ipt>');
     </script>
    <script>
        dataModel = new ht.DataModel();
        graphView = new ht.graph.GraphView(dataModel);
        // 完整厂区编码 HT_SC_ZZ_TREE_XDJSXD_XD1113
        let deptCode = GetQueryString('deptCode');
        // 截取后的厂区编码 XD1113
        let strDeptCode = deptCode.substring(deptCode.lastIndexOf("_") + 1, deptCode.length);
        let tag;

        // 监控类型
        let sceneType = GetQueryString('sceneType');
        // 车间编码
        // 0527 : 车间及报表接口传参workshopCode时，传的是截取后的XD111203,后续可能会改为完整车间编码
        let workshopCode = GetQueryString('workshopCode');
        workshopCode = workshopCode.substring(workshopCode.lastIndexOf("_") + 1, workshopCode.length);
        let backButton = '';
        let normal = '';             // 正常
        let fixedLength = '';        // 到定长
        let wireBreak = '';          // 断丝
        let changeover = '';         // 换模
        let wireReplacement = '';    // 换丝
        let breakdown = '';          // 有、无故障停机
        let offLine = '';           // 脱机
        let plannedStop = '';       // 计划停台

        let iframe = '';
        let iframeId = '';
        let iframe2 = '';
        let tableBtn = '';
        let dateTime;
        // 车间标签和报表标签均是通过node加载
        let tableBtnNode = new ht.Node();
        tableBtnNode.setTag('tableBtn');
        tableBtnNode.setPosition(818.40054,146.26163);
        tableBtnNode.s('2d.visible', true);
        tableBtnNode.s('2d.movable', false);
        tableBtnNode.s('2d.editable', false);

        if ("XD1112" === strDeptCode) {
            // tag = "workshop_eight";
            // 图纸中的报表按钮
            tableBtnNode.setImage('symbols/兴达组件/workshop/tableBtn.json');
        } else if ("XD1113" === strDeptCode) {
            // tag = "workshop_nine";
            tableBtnNode.setImage('symbols/兴达组件/workshop/tableBtn2.json');
        }

        function init() {
            graphView.addToDOM();
            ht.Default.xhrLoad('displays/智慧兴达/兴达八厂/生产监控/workshop_nine.json', function (text) {
                var json = ht.Default.parse(text);
                if (json.title) document.title = json.title;
                dataModel.deserialize(json);
                // 颜色可配置
                dataModel.a('color_config', color_config);
                dataModel.a('workshopCode', workshopCode);
                //设置编辑器地址
                dataModel.a('layout_editorURL', layout_editorURL);
                // 报表按钮添加到dataModel
                dataModel.add(tableBtnNode);
                graphView.fitContent(true);
                // 获取时间
                dateTime = dataModel.getDataByTag('dateTime');
                setInterval(function () {
                    let time = updateTime();
                    dateTime.a('time', time);
                }, 1000);
                // 获取标签
                initParams();
                // 厂区进入车间
                // let shopData = dataModel.getDataByTag(workshopCode);

                // shopData.s('onClick')(event, shopData, graphView);
                // 报警灯
                getWarningLights(workshopCode);
                // 车间返回按钮
                getWorkShopBackButton(strDeptCode, sceneType);
                // 车间按钮显示
                getDeptChange(strDeptCode, sceneType);
                setInterval("getWarningLights(workshopCode)", 1000 * 20);
            });
        }

        let messageParam = {};
        graphView.addInteractorListener(e => {
            const kind = e.kind;
            if (kind === 'clickData') {
                if (e.data._displayName === "cameraBtn") {
                    messageParam.flag = "camera";
                    messageParam.message = "true";
                    if (Boolean(e.data.a('isSelected'))) messageParam.message = "false";
                    iframeMessage(messageParam);
                } else if (e.data._displayName === "tab") {
                    // workshopCode = dataModel.a('workshopCode');
                    workshopCode = e.data.getTag();
                    getWarningLights(workshopCode);
                }
            }
        });

        // 给子页面index.html传值
        function iframeMessage(message) {
            let iframe = $('#iframe')[0];
            iframe.contentWindow.postMessage(message, '*');
        }

        function initParams() {
            workshopCode = dataModel.a('workshopCode');
            // 返回按钮
            backButton = dataModel.getDataByTag('backButton');
            // 报警灯
            normal = dataModel.getDataByTag('normal');
            fixedLength = dataModel.getDataByTag('fixedLength');
            wireBreak = dataModel.getDataByTag('wireBreak');
            changeover = dataModel.getDataByTag('changeover');
            wireReplacement = dataModel.getDataByTag('wireReplacement');
            breakdown = dataModel.getDataByTag('breakdown');
            offLine = dataModel.getDataByTag('offLine');
            plannedStop = dataModel.getDataByTag('plannedStop');

            iframe2 = dataModel.getDataByTag('iframe2');
            tableBtn = dataModel.getDataByTag('tableBtn');
            iframe = dataModel.getDataByTag('iframe');
        }


        // 返回按钮
        function workShopBackButton(backBthData) {
            if (backBthData.visible != null && backBthData.visible != undefined) {
                if ('true' === backBthData.visible) {
                    backButton.s('2d.visible', true);
                    backButton.a('url', Boolean(backBthData.data.url) ? backBthData.data.url : '');
                } else {
                    backButton.s('2d.visible', false);
                }
            }
        }

        // 报警灯
        function warningLights(lightData) {
            if (lightData.data != null && lightData.data != undefined && lightData.data != '') {
                normal.a('ht.value', Boolean(lightData.data.normal) ? lightData.data.normal : 0);
                fixedLength.a('ht.value', Boolean(lightData.data.fixedLength) ? lightData.data.fixedLength : 0);
                wireBreak.a('ht.value', Boolean(lightData.data.wireBreak) ? lightData.data.wireBreak : 0);
                changeover.a('ht.value', Boolean(lightData.data.changeover) ? lightData.data.changeover : 0);
                wireReplacement.a('ht.value', Boolean(lightData.data.wireReplacement) ? lightData.data.wireReplacement : 0);
                breakdown.a('ht.value', Boolean(lightData.data.breakdown) ? lightData.data.breakdown : 0);
                offLine.a('ht.value', Boolean(lightData.data.offLine) ? lightData.data.offLine : 0);
                plannedStop.a('ht.value', Boolean(lightData.data.plannedStop) ? lightData.data.plannedStop : 0);
            }
        }

        // 车间按钮显示
        function deptChange(deptData) {
            let positionXArr = [136.80134, 244.65801, 352.65801, 460.65801, 568.65801];
            let positionY = 145.35826;

            let data = Boolean(deptData.data) ? deptData.data : null;
            if(Boolean(data)){
                for(let _key in data){
                   let key = _key.substring(_key.lastIndexOf("_") + 1, _key.length);
                    dataModel.a('workshopCode',key);
                   let shopNode = new ht.Node();
                    shopNode.setImage('symbols/兴达组件/workshop/'+ key+'.json');
                    shopNode.a('deptName', data[_key].title);  // 车间名
                    shopNode.setTag(key);
                    shopNode.setDisplayName("tab");
                    dataModel.add(shopNode);
                    // 与 进入页面时的车间编码 一致时选中
                    if(key === workshopCode){
                        shopNode.a('isSelected',true);
                        let ifr = dataModel.getDataByTag('iframe');
                        ifr.a('iframeURL',dataModel.a('layout_editorURL')+ '/index.html?code=' + workshopCode);
                    }else{
                        shopNode.a('isSelected',false);
                    }
                    shopNode.s('2d.movable', false);
                    shopNode.s('2d.editable', false);
                    if(Boolean(data[_key].visible) && "true" === data[_key].visible) {
                        shopNode.s('2d.visible', true);
                        shopNode.setPosition(positionXArr[data[_key].flag],positionY);
                    }else{
                        shopNode.s('2d.visible', true);
                    }

                }
            }
        }

        // 接收子页面workshop_table.html传值
        let param = '';
        window.addEventListener('message', function (e) {
            param = e.data;
            tableBtn.a('isSelected', false);
            iframe2.s('2d.visible', false);
            // 给子页面index.html传值
            messageParam.flag = "table";
            messageParam.message = param;
            iframeMessage(messageParam);
        }, false);

    </script>
</head>

<body onload='setTimeout(init, 300)'>
</body>

</html>